<template>
  <div>
    <i-dialog
      :title="'添加'"
      :visible="visible"
      width="642px"
      :close-on-click-modal="false"
      @close="outerClose"
    >
      <i-form
        :items="formItems"
        :model="form"
        ref="form"
        :rules="rules"
        label-width="95px"
        class="demo-form"
      >
        <template slot="button">
          <el-form-item>
            <div>
              <el-button @click="addTimecard" type="primary">
                {{ this.timeCardVisible ? '取消' : '添加' }}时长限制</el-button
              >
              <span class="item-tip"
                >（若未添加时长限制规则，则可以无限时长使用）</span
              >
            </div>
          </el-form-item>
        </template>
        <template v-if="timeCardVisible" slot="timecard">
          <el-form-item label="时长限制" prop="initialTime">
            <div class="timecard-item">
              <el-input v-model="form.initialTime"></el-input>小时
            </div>
            <div class="timecard-item">
              <el-select v-model="form.period" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
        </template>
      </i-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outerClose">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </span>
    </i-dialog>
  </div>
</template>
<script>
import { addInternalList, addWhiteList } from '@/api/device'
import formValidate from '@/utils/validate'
export default {
  name: 'AddUser',
  data() {
    return {
      form: {},
      formItems: [
        {
          type: 'text',
          value: 'userName',
          label: '姓名',
          clearable: true,
        },
        {
          type: 'text',
          value: 'mobile',
          label: '手机号',
          clearable: true,
        },
        {
          slot: 'button',
        },
        {
          slot: 'timecard',
        },
      ],
      // period: '',
      // initialTime: '',
      timeCardVisible: false,
      options: [
        {
          value: 1,
          label: '每天',
        },
        {
          value: 2,
          label: '每周',
        },
        {
          value: 3,
          label: '每月',
        },
        {
          value: 4,
          label: '每年',
        },
      ],
      rules: {
        userName: [
          {
            validator: formValidate('validateExplanation'),
            trigger: ['change', 'blur'],
          },
        ],
        mobile: [
          {
            validator: formValidate('validateMobile'),
            trigger: ['change', 'blur'],
          },
        ],
        initialTime: [
          {
            validator: formValidate('validatePositiveInteger'),
            trigger: ['change', 'blur'],
          },
          {
            validator: (rule, value, callback) => {
              if (!/^\d*$/.test(value) || value > 9999 || value < 1) {
                return callback(new Error('请输入为1-9999之间的整数'))
              } else {
                return callback()
              }
            },
            trigger: ['change', 'blur'],
          },
        ],
      },
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    type: {
      type: String,
      required: true,
    },
    id: {
      type: [String, Number],
      required: true,
    },
  },
  methods: {
    outerClose() {
      this.form = {}
      this.period = ''
      this.initialTime = ''
      this.timeCardVisible = false
      this.$emit('update:visible', false)
    },
    addTimecard() {
      this.timeCardVisible = !this.timeCardVisible
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let timecard = {}
          if (
            this.timeCardVisible &&
            this.form.period &&
            this.form.initialTime
          ) {
            timecard = {
              date: this.form.period,
              initialTime: this.form.initialTime,
            }
          }
          let params = {
            stationId: this.id,
            whiteListInfos: [
              {
                ...this.form,
                ...timecard,
              },
            ],
          }
          const handler = {
            internal: addInternalList,
            white: addWhiteList,
          }
          handler[this.type](params).then(
            (res) => {
              if (!res.failCount) {
                this.$message({
                  message: '添加成功',
                  type: 'success',
                })
              } else {
                this.$message({
                  message: '添加失败',
                  type: 'error',
                })
              }

              this.outerClose()
            },
            () => {
              this.outerClose()
            }
          )
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.timecard-item {
  width: 50%;
  display: inline-block;
  .el-input {
    width: 80%;
  }
}
</style>
